package com.leetcode.LC_MED;

import java.util.ArrayList;
import java.util.List;

public class LC114 {
    public void flatten(TreeNode root) {

        if (root == null) {
            return;
        }
        List<TreeNode> list = new ArrayList<>();//存放的是指针地址
        preorderTraversal(root,list);
        for (int i = 1; i < list.size(); i++) {
            TreeNode prev = list.get(i - 1), curr = list.get(i);
            prev.left = null;
            prev.right = curr;
        }

    }

    public void preorderTraversal(TreeNode root, List<TreeNode> list) {
        if (root != null) {
            list.add(root);
            preorderTraversal(root.left, list);
            preorderTraversal(root.right, list);
        }
    }

}
